import { API } from "@/api/api";
import { Role, SearchRoleInput } from "@/api/api.model";
import { UseFormService } from "@/utils/useCommon/use.form.service";
import { UseTableService } from "@/utils/useCommon/use.table.service";
import { TableColumnType, TreeSelectProps } from "ant-design-vue";
import { ColumnType } from "ant-design-vue/lib/table";
import { ref } from "vue";


/////////////////表单/////////////////

export class RoleFormService extends UseFormService<Role> {

	treeData = ref<TreeSelectProps['treeData']>([]);

	private menuIdMap: any = [];
	public showForm = ref(false);

	async initForm(info?: Role): Promise<Role> {
		return {
			_id: 0,
			name: '',
			code: '',
			description: '',
		}
	}

	createRule() {
		return {
			name: [this.isRequired('角色名称')]
		}
	}

	onSubmit(info: Role, otherInfo: any) {
		this.saveData(info, (status) => {
			console.log(status);
			if (status) this.showForm.value = false;
		});
	}

	public onAdd() {
		this.modelRef._id = 0;
		this.modelRef.name = "";
		this.modelRef.code = "";
		this.showForm.value = true;
	}

	public onEdit(id) {
		this.showForm.value = true;
		this.getRoleById(id);
	}

	private getRoleById(id: number) {
		API.Query.findRoleById(id, ['_id', 'name', 'code'])
			.then((response) => {
				if (response) {
					const data = response;
					this.modelRef._id = data._id;
					this.modelRef.name = data.name;
					this.modelRef.code = data.code;
				}
			});
	}
}


/////////////////表格列表/////////////////

/**
 * 列表查找分页
 */
export class RoleTableService extends UseTableService<Role, SearchRoleInput> {
	getPageWhere(): SearchRoleInput {
		return this.paging.where as SearchRoleInput;
	}
	/**
	 * 获取表格列
	 * @date 2022-03-10
	 * @return {any}
	 */
	public columns(): TableColumnType<{}>[] {
		const columns: ColumnType[] = [
			{ ...this.getColumns('序号', 'rowIndex') },
			{ ...this.getColumns('名称', 'name') },
			{ ...this.getColumns('操作', 'action'), width: 150, fixed: 'right' },
		]
		return columns
	}
}